<?php
namespace Home\Controller;
header("Content-type: text/html; charset=utf-8");
use Think\Controller;
ini_set("memory_limit","40000M");
ini_set('max_execution_time', '0');
//
class IndexController extends Controller
{

    /**
     * 初始化链
     *
     * @descript 如果未登录跳转至登录页面
     */
	function _initialize(){

// 		dump(implode("",explode(".","1.5.1.10615")));die;
		
//         if (!session('?user')) {
//             $this->redirect('/Home/Login/login');
//         }

    }
    public function handlekeywords1(){
    	$file =M('file');
    	$cnt = $file->count();
    	$pagecnt = 10;
    	for($i=0;$i<$cnt;$i++){
    		$data = $file->limit($i*$pagecnt,$pagecnt)->field('id,keywords,type')->select();
    		foreach ($data as $v){
    			if($v['type']==1){
    				if($v['keywords']){
    					$t1 =urldecode($v['keywords']);
    					if($v['keywords']!="%5B%22%E5%85%A8%E6%99%AF%E8%A7%86%E9%A2%91%22%5D"){
    						$temp = urlencode(str_replace("\"全景视频\"","",$t1));
    						$sdata['id']=$v['id'];
    						$sdata['keywords']=$temp;
    						$file->save($sdata);
    					}
    				}
    
    			}else{
    				if($v['keywords']){
    					$t1 =urldecode($v['keywords']);
    					if($t1!="[\"全景图片\"]"){
    						$temp = urlencode(str_replace("\"全景图片\"","",$t1));
    						$sdata['id']=$v['id'];
    						$sdata['keywords']=$temp;
    						$file->save($sdata);
    					}
    				}
    
    			}
    			
    		}
    	}
    
    }
    
	public function handlekeywords(){
		$file =M('file');
		$cnt = $file->count();
		$pagecnt = 10;
		for($i=0;$i<$cnt;$i++){
			$data = $file->limit($i*$pagecnt,$pagecnt)->field('id,keywords,type')->select();
			 foreach ($data as $v){
			 	if($v['type']==1){
			 		if($v['keywords']){
			 			$t1 =urldecode($v['keywords']);
			 			if(!strstr($t1,'全景视频')){
			 				$temp = urlencode("[\"全景视频\"".str_replace("[","",$t1));
			 			}
			 		}else{
			 				$temp = urlencode("[\"全景视频\"]");
			 		}

			 	}else{
			 		if($v['keywords']){
			 			$t1 =urldecode($v['keywords']);
			 			if(!strstr($t1,'全景图片')){
			 				$temp = urlencode("[\"全景图片\"".str_replace("[","",$t1));
			 			}
			 		}else{
			 			$temp = urlencode("[\"全景图片\"]");
			 		}
			 		
			 	}
			 	$sdata['id']=$v['id'];
			 	$sdata['keywords']=$temp;
			 	$file->save($sdata);
			 }
		}
		
	}
    public function zz(){
    	$where['type']=1;
    	$file = M("file");
    	$cnt = $file->where("`playtime`='' and type=1")->count();
    	$pagecnt = 10;
    	for ($i=0;$i<$cnt/$pagecnt;$i++){
    		$data = $file->where("`playtime`='' and type=1")->limit($i*$pagecnt,$pagecnt)->field('filename,url,id,playtime')->select();
    	
    		foreach ($data as $v){
    			$output ="";
    			$mp3 = $v["url"].$v["filename"]."_1440x720_libx264_2000000.mp4";
    			exec('/usr/local/bin/ffmpeg -i '.$mp3.' 2>&1 | grep "Duration" | cut -d " " -f 4 | sed s/,//', $output, $return_val);
    			echo "==================";
    			dump($output[0]);
    			echo "==================";
    			if(!empty($output[0])){
    				$arr1 = explode('.', $output[0]);
    				$arr  =explode(':',$arr1[0]);
    				$len = count($arr);
    				$res = (int)$arr[$len - 1] + (int)$arr[$len - 2] * 60 + (($len > 2 ? (int)$arr[$len - 3] : 0) * 60 * 60);
    				$d['playtime']=$res;
    				$d['id']=$v['id'];
    				$res = $file->save($d);
    				dump($res);
    				dump("成功！");
    			}else{
    				dump("播放时间为0");
    			}
    		}		
    	}
    }			
    
    function getImageInfo($img) { //$img为图象文件绝对路径
    	$img_info = getimagesize($img);
    	switch ($img_info[2]) {
    		case 1:
    			$imgtype = "GIF";
    			break;
    		case 2:
    			$imgtype = "JPG";
    			break;
    		case 3:
    			$imgtype = "PNG";
    			break;
    			$img_type = $imgtype."图像";
    			$img_size = ceil(filesize($img)/1000)."k"; //获取文件大小
    			$new_img_info = array (
    					"width"=>$img_info[0],
    					"height"=>$img_info[1],
    					"type"=>$img_type,
    					"size"=>$img_size
    			);
    			print " width";
    			print $img_info[0];
    			print " height";
    			print $img_info[1];
    			print " format";
    			print $img_type;
    			print " size";
    			print $img_size;
    			return $new_img_info;
    	}
    	//以下为测试用
    	$img="/home/pics/title.gif";
    	getImageInfo($img);
    
    }
    public function handlepic(){
//     	$res = M('file')->where('picsize ='."")->select();
//     	foreach ($res as $val){
    		
//     	}
//     	dump(getimagesize("https://awsbj0-cdn.fds-ssl.api.xiaomi.com/madv360/02.jpg"));
//     	dump(filesize("https://awsbj0-cdn.fds-ssl.api.xiaomi.com/madv360/02.jpg"));
		$file = M("file");
    	$cnt = $file->where("`picsize`=''")->count();
    	$pagecnt = 10;
    	for ($i=0;$i<$cnt/$pagecnt;$i++){
    		$data = $file->where("`picsize`=''")->limit($i*$pagecnt,$pagecnt)->field('filename,url,id,picsize')->select();
    		
    		foreach ($data as $v){
    			$pic = $v["url"].$v["filename"].".png";
    			$size = file_get_contents($pic);
    			$d['picsize']=strlen($size);
    			$d['id']=$v['id'];
    			$res = $file->save($d);
    			dump($res);
    		}
    	}
    	echo 'done';
    }
	
    /**
     * 首页的数据
     */
	public function index(){
	    if(strstr($_SESSION['aut'],'1')){
	        $_SESSION['index'] = '1';
        } else{
	        $_SESSION['index'] = '-1';
        }
        $this->display();
	}

	public function add(){
		$pub = M('publish');
		$data['sys']=$_POST['sys'];
		$data['version']=$_POST['version'];
		$data['doc']=$_POST['doc'];
		$data['size']=$_POST['size'];
		$data['sysinfo']=$_POST['sysinfo'];
		$data['hard_updtime']="";
		$data['hard_upgtime']="";
		$data['createtime']=time();
		$data['status']=1;
		
		
		$data['url']=$_POST['url'];
		
		
		$str=$_POST['doc'];
		$arr=explode("\n",$str);
 		$str1=nl2br($str);//回车换成换行
 		
 		$data['doc']=$str1;
// 		echo $str1;
		
		
		$res = $pub->add($data);
		if($res){
			$res =$pub->where('status =1')->order('id desc')->find();
			$this->assign(res,$res);
			
			
			$black =M("whitetest")->where('type=1')->find();
			$this->assign(black,$black['name']);
			$this->display('white');
			
		}else{
			$this->error("数据添加失败！");
		}
	}

	public function upload(){
        $upload = M('publish');
        session_start();
        $data['sys'] = $_POST['sys'];
        $data['version'] = $_POST['version'];
        $data['size'] = $_FILES['file']['size'];
        $data['doc'] = $_POST['doc'];
        $data['sysinfo'] = $_POST['sysinfo'];
        $data['createtime'] = time();
        $data['status'] = 1;
//        echo '文件名:' . $_FILES['file']['name'] . '</br>';
//        echo '文件大小:' . $_FILES['file']['size'] . 'KB</br>';
//        echo '文件临时存储位置:' . $_FILES['file']['tmp_name'] . '</br>';
//        echo '文件转存位置:' .  __DIR__ . "/../../../crontabscript/examples/" . $_FILES['file']['name']. '</br>';
        $_SESSION['filename'] = $_FILES['file']['name'];
        $file_path = __DIR__ . '/../../../crontabscript/examples/' . $_FILES['file']['name'];
        if(is_uploaded_file($_FILES['file']['tmp_name'])){
            if (move_uploaded_file( $_FILES['file']['tmp_name'] , $file_path )){
                $file_contents = file_get_contents($file_path);
                $_SESSION['filecontent'] = $file_contents;
                unlink(__DIR__ . '/../../../crontabscript/examples/' . $_FILES['file']['name']);
//                echo '文件转存内容:' . $file_contents . '</br>';
                include_once(__DIR__ ."/../../../crontabscript/examples/galaxy-fds.php");
                $data['url'] = $_SESSION['download'];
                $res = $upload->add($data);
                if(SendMail('15652915497@163.com','后台邮件测试','邮箱测试专用'))
                    $this->success('邮件发送成功');
                else
                    $this->error('邮件发送失败');
                echo "<p>上传成功</p>";
                echo $data['url'];
            }
            else{
                echo '文件转存失败';
            }
        }   else{
            echo '文件传输出错，非POST模式';
        }



    }
	
	public function confirm(){
        if(strstr($_SESSION['aut'],'3')){
            $_SESSION['confirm'] = '1';
        } else {
            $_SESSION['confirm'] = '-1';
        }
        $conn = M("publish");
        $page = new \Think\Page($conn->where('status = 2 AND sys = (1 OR 2)')->count(),10);
        $data = $conn
            ->where('status = 2 AND sys = (1 OR 2)')
            ->order('createtime DESC')
            ->limit($page->firstRow. ',' .$page->listRows)
            ->select();
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('data',$data);
        $this->assign('_page',$page->show());
        $this->display();
	}

	public function confirm_upload(){
	    $pub = M("publish");
	    $data['status'] = '3';
	    $data['createtime'] = time();
	    $res = $pub->where('id='.$_POST['id'])->save($data);
	    if ($res){
	        $data['status'] = '1';
        } else{
	        $data['status'] = '-1';
	    }
	    $this->ajaxReturn($data);
    }

    public function confirm_shelf(){
        $pub = M("publish");
        $data['status']=1;
        $res =$pub->where('id='.$_POST['id'])->save($data);
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }

	public function pubshow(){
        if(strstr($_SESSION['aut'],'4')){
            $_SESSION['pubshow'] = '1';
        } else {
            $_SESSION['pubshow'] = '-1';
        }
		$conn = M("publish");
        $page = new \Think\Page($conn->where('status = 3 AND sys=(1 OR 2)')->count(),10);
        $data = $conn
            ->where('status = 3')
            ->order('createtime DESC')
            ->limit($page->firstRow. ',' .$page->listRows)
            ->select();
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('data',$data);
        $this->assign('_page',$page->show());
        $this->display();
	}



	public function release(){
		$pub = M("publish");
		$where['status']=3;
		$where['id']=$_POST['id'];
		$data =$pub->save($where);
		if($data){
			$this->success('发布成功',U('pubshow'));
		}else{
			$this->error('发布失败');
		}
		
	}

	public function remove(){
		$res =M('publish')->where('id='.$_POST['id'])->delete();
		if($res){
			$data['status']=1;
		}else{
			$data['status']=-1;
		}
		$this->ajaxReturn($data);
	}

	public function shelf(){
		$where['status']=2;
		$res =M('publish')->where('id='.$_POST['id'])->save($where);
		if($res){
			$data['status']=1;
		}else{
			$data['status']=-1;
		}
		$this->ajaxReturn($data);
	}



	public function logout(){
		unset ($_SESSION['user']);
	}


	public function white(){
        if(strstr($_SESSION['aut'],'2')){
            $_SESSION['white'] = '1';
        } else {
            $_SESSION['white'] = '-1';
        }
	    $conn = M('publish');
        $page = new \Think\Page($conn->where('status = 1 AND sys=(1 OR 2)')->count(),10);
	    $data = $conn
            ->where('status = 1 AND sys=(1 OR 2)')
            ->order('createtime DESC')
            ->limit($page->firstRow. ',' .$page->listRows)
            ->select();
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
	    $this->assign('data',$data);
        $this->assign('_page',$page->show());
        $this->display();
	}

	public function white_upload(){
        $upload = M('publish');
        $data['doc'] = $_POST['doc'];
        $data['sysinfo'] = $_POST['sysinfo'];
        $data['createtime'] = time();
        $data['status'] = 2;
        $res = $upload->where('id ='.$_POST['id'])->save($data);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        $this->ajaxReturn($data);
    }

    public function whitelist(){
        if(strstr($_SESSION['aut'],'9')){
            $_SESSION['whitelist'] = '1';
        } else {
            $_SESSION['whitelist'] = '-1';
        }
        $conn=M('whitelist');
        if(!$conn){
            echo "连接失败";
        }
        $page = new \Think\Page($conn->where('type = 1')->count(),10);
        $ls =$conn
            ->where('type = 1')
            ->order('time DESC')
            ->limit($page->firstRow. ',' .$page->listRows)
            ->select();
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('data',$ls);
        $this->assign('_page',$page->show());
        $this->display();
    }

    public  function white_add(){
        $conn=M('whitelist');
        if(!$conn){
            echo "连接失败";
        }
        $data['name'] = $_POST['name'];
        $data['type'] = $_POST['type'];
        $data['serial'] = $_POST['serial'];
        $data['model'] = $_POST['model'];
        $data['person'] = $_POST['person'];
        $data['time'] = time();
        $res = $conn->add($data);
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
	}

    public  function white_save(){
        $conn=M('whitelist');
        if(!$conn){
            echo "连接失败";
        }
        $data['name'] = $_POST['name'];
        $data['type'] = $_POST['type'];
        $data['serial'] = $_POST['serial'];
        $data['model'] = $_POST['model'];
        $data['person'] = $_POST['person'];
        $data['time'] = time();
        $res = $conn->where('id='.$_POST['id'])->save($data);
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }


    public function white_del(){
        $res =M('whitelist')->where('id='.$_POST['id'])->delete();
        if($res){
            $data['status']=1;
        }else{
            $data['status']=-1;
        }
        $this->ajaxReturn($data);
    }



}